﻿using Microsoft.AspNetCore.Mvc.Abstractions;
using Microsoft.AspNetCore.Mvc.Filters;
using My.WebApi.Middlewares;
using Newtonsoft.Json;
using System.Reflection.Metadata;
using Volo.Abp;

namespace My.WebApi.Filters
{
    public class UserLoginStatusFilter : IAsyncActionFilter
    {
        public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
        {
            var methodInfo = context.ActionDescriptor.GetMethodInfo();
            var customAttribute = methodInfo.GetCustomAttributes(typeof(NotCheckUserLoginStatusAttribute), true)
            .FirstOrDefault() as NotCheckUserLoginStatusAttribute;

            var uniqueId = context.HttpContext.Request.Headers["UniqueId"].ToString();
            if (!uniqueId.IsNullOrWhiteSpace())
            {
                var uniqueIdCache = "123";
                if (uniqueIdCache != uniqueId)
                {
                    throw new UserFriendlyException(message: "用户已经在别处登录", code: "50000");
                }
            }
            await next();
        }
    }

    [AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
    public class NotCheckUserLoginStatusAttribute : Attribute
    {
    }
}
